package com.yztl.controller;


import com.alibaba.fastjson.JSON;
import com.yztl.pojo.Admin;
import com.yztl.pojo.Client;
import com.yztl.pojo.Order;
import com.yztl.service.Client.ClientService;
import com.yztl.service.admin.AdminService;
import com.yztl.util.PageUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/client")
public class ClientController {

    @Autowired
    ClientService clientService;
    @Autowired
    AdminService adminService;


    @RequestMapping("/login.html")
    public String login(){
        return "ClientLogin";
    }

    @PostMapping("/doLogin.html")
    public String doLogin(@RequestParam String username,
                          @RequestParam String password,
                          HttpSession session){
        Client clientlogin = clientService.Clientlogin(username, password);
        if(clientlogin!=null){
            session.setAttribute("Client",clientlogin);
            session.removeAttribute("error");
            return "redirect:/client/main.html";
        }else{
            session.setAttribute("error","账号或密码错误");
            return "ClientLogin";
        }

    }
    @RequestMapping("/main.html")
    public String main(HttpSession session){
        if(session.getAttribute("Client")==null){
            return "ClientLogin";
        }
        return "ShowOrder";
    }

    @RequestMapping("/over.html")
    public String over(HttpSession session){
        session.removeAttribute("Client");
        session.removeAttribute("error");
        return "index";
    }

    @RequestMapping("/getPageProduct.html")
    @ResponseBody
    public String getPageProduct(@Param("pageIndex") String pageIndex, HttpSession session){
            if(pageIndex==null ){
                pageIndex="1";
            }
            int index=Integer.parseInt(pageIndex);
            if(index<=1){
                index=1;
            }

        Client client = (Client) session.getAttribute("Client");
        PageUtil<Order> pageUtil = new PageUtil<>();
        pageUtil.setPageIndex(index);
        pageUtil.setPageSize(3);
        clientService.getPageProduct(pageUtil,client.getId());
        List<Order> lists = pageUtil.getLists();
        session.setAttribute("list",lists);
        return JSON.toJSONString(pageUtil);
    }

    @RequestMapping("/delOrder.html")
    @ResponseBody
    public String delOrder(int id,int state){
        return JSON.toJSONString(clientService.deleteOrder(id,state));
    }

    @RequestMapping("/getProduct.html")
    @ResponseBody
    public String getProduct(){
        return JSON.toJSONString(adminService.getProduct());
    }


    @RequestMapping("/goSelOne.html")
    public String goSelOne(HttpSession session,int id){
        session.setAttribute("id",id);
        return "updOrder";
    }

    @RequestMapping("/selOne.html")
    @ResponseBody
    public String selOne(int id){
        return JSON.toJSONString(clientService.selOneOrder(id));
    }

    @RequestMapping("/upd.html")
    public String upd(
                HttpSession session,
                int product_name,
                int product_num
    ){
        int order_id = (int) session.getAttribute("id");
        if (clientService.updateOrder(new Order(order_id,product_name,product_num,new Date()))!=0) {
            return "ShowOrder";
        }else{
            return "updClient";
        }

    }

    @RequestMapping("/goAdd.html")
    public String goAdd(){
        return "AddOrder";
    }

    @RequestMapping("/add.html")
    public String add(
            HttpSession session,
            int product_name,
            int product_num
    ){
        Client client = (Client) session.getAttribute("Client");
    if(clientService.addOrder(new Order(client.getId(),product_name,product_num,new Date(),null,1))!=0){
        return "ShowOrder";
    }else{
        return "AddOrder";
    }
    }

}
